<template>
  <div class="dashboard-container">
    <el-row type="flex" justify="end">
      <el-col :span="6" style="text-align: right">
        <el-form :inline="true" class="form-inline" style="margin-top: 20px;margin-bottom: -20px;padding-right: 20px">
          <el-form-item label="统计时间范围">
            <el-select v-model="hoursAgo" placeholder="统计时间" default-first-option class="filter-item" style="width: 150px" @change="handleStat">
              <el-option v-for="item in dateRangeOtions" :key="item.hour" :label="item.name" :value="item.hour"/>
            </el-select>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
    <el-row type="flex" class="panel-group" justify="center">
      <el-col v-for="stat in formatData" :span="6" :key="stat.name" class="card-panel-col">
        <el-card :body-style="{ padding: '2px' }">
          <div slot="header" class="clearfix">
            <span class="card-panel-text">{{ stat.name }}</span>
            <div class="header_info">
              <el-tag type="success"><count-to :start-val="0" :end-val="stat.okCount" :duration="500" class="card-panel-num" prefix="" suffix=""/></el-tag>
              /
              <el-tag type="danger"><count-to :start-val="0" :end-val="stat.errorCount" :duration="500" class="card-panel-num" prefix="" suffix=""/></el-tag>
            </div>
          </div>
          <div v-for="system in stat.children" :key="system.name" class="card-panel" @click="handleSubStat(system)">
            <div :class="errorRate(system)<0.01?'card-panel-icon-wrapper icon-success':'card-panel-icon-wrapper icon-warning'">
              <svg-icon :icon-class="errorRate(system)<0.01?'success':'warning'" class-name="card-panel-icon" />
              <div style="text-align: center;padding-top: 55px;">
                {{ (errorRate(system)*100).toFixed(2) }}%
              </div>
            </div>
            <div class="card-panel-description">
              <div class="card-panel-text">{{ system.name }}</div>
              <el-tag type="success"><count-to :start-val="0" :end-val="system.okCount" :duration="500" class="card-panel-num" prefix="" suffix=""/></el-tag>
              /
              <el-tag type="danger"><count-to :start-val="0" :end-val="system.errorCount" :duration="500" class="card-panel-num" prefix="" suffix=""/></el-tag>
            </div>
          </div>
        </el-card>
      </el-col>
    </el-row>

    <el-dialog :title="subStatTitle" :visible.sync="subStatVisible" top="2vh">
      <el-table :data="subStatData" :border="true" :row-class-name="subStatTableRowClass">
        <el-table-column property="name" label="子模块" width=""/>
        <el-table-column property="okCount" label="成功次数" width=""/>
        <el-table-column property="errorCount" label="异常次数" width=""/>
      </el-table>
    </el-dialog>
  </div>
</template>
<style>
  .el-table .warning-row {
    background: oldlace;
  }

  .el-table .success-row {
    background: #f0f9eb;
  }
</style>
<script>
import { mapGetters } from 'vuex'
import CountTo from 'vue-count-to'
import { stat } from '@/api/stat'
import { parseTime } from '@/utils'

export default {
  name: 'Dashboard',
  components: { CountTo },
  data() {
    return {
      statCondition: {
        platformName: '',
        clientName: '',
        serviceCategory: '',
        serviceName: '',
        env: '',
        fromDate: '',
        toDate: ''
      },
      statData: [

      ],
      hoursAgo: 24,
      dateRangeOtions: [
        { 'name': '最近1小时', 'hour': 1 },
        { 'name': '最近4小时', 'hour': 4 },
        { 'name': '最近8小时', 'hour': 8 },
        { 'name': '最近12小时', 'hour': 12 },
        { 'name': '最近1天', 'hour': 24 }
      ],
      timer: undefined,
      subStatTitle: '',
      subStatVisible: false,
      subStatData: []
    }
  },
  computed: {
    ...mapGetters([
      'roles'
    ]),

    // 格式化数据源
    formatData: function() {
      const data = []
      for (let i = 0; i < this.statData.length; i++) {
        const stat = this.statData[i]
        const children = []
        for (let j = 0; j < stat.children.length; j++) {
          const system = stat.children[j]
          const subStatData = []
          if (system.children.length > 0) {
            for (let k = 0; k < system.children.length; k++) {
              const subSys = system.children[k]
              subStatData.push(
                {
                  name: subSys.name,
                  okCount: subSys.okCount,
                  errorCount: subSys.errorCount
                }
              )
            }
          }
          children.push(
            {
              name: system.name,
              okCount: system.okCount,
              errorCount: system.errorCount,
              subStatData: subStatData
            }
          )
        }
        data.push({
          name: stat.name,
          okCount: stat.okCount,
          errorCount: stat.errorCount,
          children
        })
      }
      return data
    }
  },
  created() {
  },
  mounted() {
    this.handleStat()
    if (this.timer) {
      clearInterval(this.timer)
    } else {
      this.timer = setInterval(() => {
        this.handleStat()
      }, 10000)
    }
  },
  destroyed() {
    clearInterval(this.timer)
  },
  methods: {
    handleStat() {
      const day = new Date()
      if (this.hoursAgo > 0) {
        day.setTime(day.getTime() - 1000 * 60 * 60 * this.hoursAgo)
      }
      this.statCondition.fromDate = parseTime(day)

      stat('statSunburst', this.statCondition).then(response => {
        if (response.code === '0000') {
          this.statData = response.data
        }
      })
    },
    errorRate(stat) {
      let rate = 0
      if (stat.okCount > 0) {
        rate = stat.errorCount / stat.okCount
      }
      return rate
    },
    handleSubStat(system) {
      this.subStatTitle = system.name + '模块统计'
      this.subStatData = system.subStatData
      this.subStatVisible = true
    },
    subStatTableRowClass({ row, rowIndex }) {
      console.info(row.errorCount)
      if (row.errorCount > 0) {
        return 'warning-row'
      }
      return ''
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  .panel-group {
    margin-top: 18px;
    .card-panel-col{
      margin-bottom: 32px;
      margin-left: 20px;
    }
    .card-panel {
      height: 108px;
      cursor: pointer;
      font-size: 12px;
      position: relative;
      overflow: hidden;
      color: #666;
      background: #fff;
      box-shadow: 4px 4px 40px rgba(0, 0, 0, .05);
      border-color: rgba(0, 0, 0, .05);
      margin-bottom: 0px;
      &:hover {
        .card-panel-icon-wrapper {
          color: #fff;
        }
        .icon-success {
          background: #67C23A;
        }
        .icon-warning {
          background: #E6A23C;
        }
      }
      .icon-success {
        color: #67C23A;
      }
      .icon-warning {
        color: #E6A23C;
      }
      .card-panel-icon-wrapper {
        float: left;
        margin: 14px 0 0 14px;
        padding: 16px;
        transition: all 0.38s ease-out;
        border-radius: 6px;
      }
      .card-panel-icon {
        float: left;
        font-size: 48px;
      }
      .card-panel-description {
        float: right;
        font-weight: bold;
        margin: 26px;
        margin-left: 0px;
        text-align: right;
        .card-panel-text {
          line-height: 18px;
          text-align: right;
          color: rgba(0, 0, 0, 0.45);
          font-size: 18px;
          margin-bottom: 12px;
        }
        .card-panel-num {
          font-size: 20px;
        }
      }
    }
    .card-panel-text {
      line-height: 30px;
      color: rgba(0, 0, 0, 0.45);
      font-size: 22px;
      margin-bottom: 12px;
    }
    .header_info{
      display: inline;
      float: right;
      line-height: 30px;
    }
  }
</style>
